package modules.lock.lock2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 *
 * 票 - 资源类
 *  Lock三部曲
 *   1、 new ReentrantLock();
 *   2、 lock.lock(); // 加锁
 *   3、 finally=> lock.unlock(); // 解锁
 * @author shengren.yan
 * @create 2021-03-05
 */
public class Ticket2 {

    private int number = 30;

    // 可重入锁
    Lock lock = new ReentrantLock();
    // 卖票的方法
    public  void doTicket(){
        //加锁
        lock.lock();
        try {
            if(number > 0){
                System.out.println(Thread.currentThread().getName() + " 线程：卖出第" + (number--) +  ",还有这些票：" + number);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 解锁
            lock.unlock();
        }
    }


}